<?php

/*
 * Edit by shengxin
 * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
 */

namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use api\mall\model\TaskConfigModel;
use api\mall\model\MedalModel;
/**
 * Description of TaskController
 *
 * @author PC
 */
class TaskController extends AdminBaseController{
    private function task_type(){
        return ['AccumulateLogin'=>'累计登录','MassDigital'=>'藏品满屋','MassFriends'=>'好友成群','MassShopping'=>'购物达人','PromoteTask'=>'推广任务'];
    }
    private function task_type_daily(){
        return ['OnlineTask'=>'在线时长','SignInTask'=>'每日签到','TalkAllTask'=>'文明发言','TalkTask'=>'文明私聊','CuriosityTask'=>'好奇宝宝'];
    }
    
    public function sum(){
        $model = new \api\user\model\UserScoreLogModel();
        $sum = $model->where('type','task')->field('sum(score) sum_score,sum(coin) sum_coin')->find();
        $this->assign($sum->toArray());
        $valve = cmf_get_option('mall_task_valve');
        if(empty($valve)){
            $valve = ['max'=>4000000];
        }
        $this->assign($valve);
        return $this->fetch();
    }
    
    public function index(){
        $model = new TaskConfigModel();
        $items=$model->where('task_type','<>',1)->paginate(20);
        $list = [];
        $type= $this->task_type();
        foreach ($items as $item){
            $key = $item['task_key'];
            $item['name']=isset($type[$key])?$type[$key]:'未知';
            $list[] = $item;
        }
        
        $this->assign('items',$list);
        $this->assign('page',$items->render());
        return $this->fetch();
    }
    
    public function add(){
        $id = $this->request->route('id/d',0);
        $sel = '';
        if($id>0){
            $model = new TaskConfigModel();
            $task = $model->where('id',$id)->find();
            if($task){
                $this->assign($task->toArray());
                $sel = $task['task_key'];
            }
        }
        $sels = [];
        $typs = $this->task_type();
        foreach ($typs as $key=>$val){
            if($key==$sel){
                $select = '<option value="'.$key.'" selected>'.$val.'</option>';
            }else{
                $select = '<option value="'.$key.'">'.$val.'</option>';
            }
            $sels[] = $select;
        }
        $this->assign('tasktypes',$sels);
        $this->assign('task_type',0);
        return $this->fetch();
    }
    
    public function index_daily(){
        $model = new TaskConfigModel();
        $items=$model->where('task_type',1)->paginate(20);
        $list = [];
        $type= $this->task_type_daily();
        foreach ($items as $item){
            $key = $item['task_key'];
            $item['name']=isset($type[$key])?$type[$key]:'未知';
            $list[] = $item;
        }
        
        $this->assign('items',$list);
        $this->assign('page',$items->render());
        return $this->fetch('index_daily');
    }
    
    public function add_daily(){
        $id = $this->request->route('id/d',0);
        $sel = '';
        if($id>0){
            $model = new TaskConfigModel();
            $task = $model->where('id',$id)->find();
            if($task){
                $this->assign($task->toArray());
                $sel = $task['task_key'];
            }
        }
        $sels = [];
        $typs = $this->task_type_daily();
        foreach ($typs as $key=>$val){
            if($key==$sel){
                $select = '<option value="'.$key.'" selected>'.$val.'</option>';
            }else{
                $select = '<option value="'.$key.'">'.$val.'</option>';
            }
            $sels[] = $select;
        }
        $this->assign('tasktypes',$sels);
        $this->assign('task_type',1);
        return $this->fetch('add_daily');
    }
    
    public function sign_in(){
        $task_config = cmf_get_option('mall_sign_in');
        if(empty($task_config)){
            $task_config = [1,1,1,1,1,1,1];
        }
        $this->assign('week',['周一','周二','周三','周四','周五','周六','周日']);
        $this->assign('rewards',$task_config);
        return $this->fetch();
    }
    
    public function medal_list(){
        $model = new MedalModel();
        $key = $this->request->route('key/d',2);
        if($key==2){
            $list = $model->select();
        }else{
            $list = $model->where('medal_key',$key)->select();
        }
        
        $this->assign('medals',$list);
        return $this->fetch();
    }
    
    public function add_medal(){
        $id = $this->request->route('id/d',0);
       
        if($id>0){
            $model = new MedalModel();
            $list = $model->where('id',$id)->find();
            $this->assign($list->toArray());
        }else{
            $this->assign('medal_key',0);
        }
        return $this->fetch();
    }    
    
    public function medal_info(){
        $id = $this->request->get('id/d',0);
        if($id>0){
            $model = new MedalModel();
            $list = $model->where('id',$id)->find();
            if($list){
                $list['medal_url']= cmf_get_image_preview_url($list['medal_url']);
            }
            $this->success('ok',null,$list);
        }
        $this->success('ok');
    }

}
